﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class TerrainLib : Form
    {
        TerrainElementEditor terrainElementEditor;
        public CTerrainLibList terrainList;
        String libFile;

        public TerrainLib()
        {
            InitializeComponent();
            terrainElementEditor = new TerrainElementEditor();
            terrainList = new CTerrainLibList();
            libFile = "";
        }

        private void InitListView()
        {
            TerrainList.LargeImageList = TerrainImageList;
            TerrainList.View = View.LargeIcon;
            TerrainImageList.ImageSize = new Size(64, 64);

            for ( int i = 0; i < terrainList.pElements.Count;i ++ )
            {
                TerrainImageList.Images.Add(((CTerrainElement)terrainList.pElements[i]).bmpTerrainElement);
                TerrainList.Items.Add(((CTerrainElement)terrainList.pElements[i]).mFileName, i);
            }

            TerrainList.Invalidate();
        }

        private void TerrainList_DoubleClick(object sender, EventArgs e)
        {
            return;
        }

        private void TerrainList_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                DoDragDrop(e.Item.ToString(), DragDropEffects.Copy | DragDropEffects.Move);
            }
        }

        private void TerrainList_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (terrainElementEditor.IsDisposed )
            {
                terrainElementEditor = new TerrainElementEditor();
            }
            terrainElementEditor.Visible = true;
        }

        private void ReadLib_Click(object sender, EventArgs e)
        {
            if ( libFile == "" )
            {
                openTerrainLib.Filter = CONST.XMLFILTER;

                if (openTerrainLib.ShowDialog() == DialogResult.OK)
                {
                    terrainList.Read(openTerrainLib.FileName);
                    libFile = openTerrainLib.FileName;

                    InitListView();
                }
            }
            else
            {
                MessageBox.Show("不能重新打开地表库！");
            }
        }

        private void TerrainLib_FormClosing(object sender, FormClosingEventArgs e)
        {
            Program.mainWindow.UncheckMenuTerrainLib();

            if ( terrainElementEditor == null )
            {
                return;
            }
            if ( terrainElementEditor.IsDisposed )
            {
                return;
            }
            e.Cancel = true;
            this.Visible = false;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            saveTerrainLib.Filter = CONST.XMLFILTER;

            if (libFile == "")
            {
                if (saveTerrainLib.ShowDialog() == DialogResult.OK)
                {
                    terrainList.Save(saveTerrainLib.FileName);
                }
            }
            else
            {
                terrainList.Save(libFile);
            }
        }

    }
}
